//There is a bug in serialize form it don't work for the multiselect elemets!!! link:https://prototype.lighthouseapp.com/projects/8886/tickets/1180-formserialize-is-broken-for-multiple-selects
function serializePersonForm()
{
    var res="";
    if($F('uid').toString()!=0){res='id='+$F('uid')+'&';}
    if($F('fname').toString()!=0){res+='fName='+$F('fname')+'&';}
    if($F('lname').toString()!=0){res+='lName='+$F('lname')+'&';}
    if($F('age').toString()!=0){res+='age='+$F('age')+'&';}
    if($F('sex').toString()!=0){res+='sex='+$F('sex')+'&';}
   res= res.slice(0, -1);
    return res;
}

function showTable()
{
    new Ajax.Request
        (
            'get/allpersons',
            {
            method:'get',
            onComplete:showAllPersons
            }
    );
}

function showAllPersons(oReq)
{
    var table = oReq.responseText.toString();
    table = table.replace('[', '');
    table = table.replace(']', '');
    var data = table.split(',');
    var res = "<table border='1'><tr><td>id</td><td>First name</td><td>Last name</td><td>Age</td><td>Sex</td></tr>";

    for (var i = 0; i < data.length; i++)
    {
        var person = data[i].split(';');
        res += "<tr>";
        for (var j = 0; j < person.length; j++)
        {
            res += "<td>" + person[j] + "</td>";
        }
        res += "</tr>";
    }

    res += "<form id='personForm'>" +
        "<tr><td><input type='text' id='uid' /></td>" +
        "<td><input type='text' id='fname' /></td>" +
        "<td><input type='text' id='lname' /></td>" +
        "<td><input type='text' id='age' /></td>" +
        " <td><select id='sex'>" +
        " <option value='M'>Male</option>" +
        " <option value='F'>Female</option>" +
        "</select></td>" +
        " </tr>" +
        " </form></table>";

    $('showPersons').update(res);
}

function findPerson()
{
    sendForm( 'get/person',serializePersonForm(),showAllPersons);
}

